package channel

import (
	"context"

	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type TenantChannelKbzpayModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewTenantChannelKbzpayModel(ctx context.Context, db *gorm.DB) *TenantChannelKbzpayModel {
	return &TenantChannelKbzpayModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *TenantChannelKbzpayModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *TenantChannelKbzpayModel) CreateKbzpay(kbzpay *schema.TenantChannelKbzpay) error {
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Create(kbzpay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-CreateKbzpay,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelKbzpayModel) GetKbzpayById(id int64) (*schema.TenantChannelKbzpay, error) {
	var kbzpay schema.TenantChannelKbzpay
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Where("id = ?", id).First(&kbzpay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetKbzpayById,err", err)
		return nil, err
	}
	return &kbzpay, nil
}

func (model *TenantChannelKbzpayModel) GetKbzpayByCode(code string) ([]schema.TenantChannelKbzpay, error) {
	var kbzpayList []schema.TenantChannelKbzpay
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Where("code = ?", code).Find(&kbzpayList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetKbzpayByCode,err", err)
		return nil, err
	}
	return kbzpayList, nil
}

func (model *TenantChannelKbzpayModel) UpdateKbzpay(kbzpay *schema.TenantChannelKbzpay) error {
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Where("id = ?", kbzpay.ID).Updates(kbzpay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateKbzpay,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelKbzpayModel) DeleteKbzpay(id int64) error {
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Where("id = ?", id).Delete(&schema.TenantChannelKbzpay{})
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-DeleteKbzpay,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelKbzpayModel) GetAllKbzpay() ([]schema.TenantChannelKbzpay, error) {
	var kbzpayList []schema.TenantChannelKbzpay
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Find(&kbzpayList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetAllKbzpay,err", err)
		return nil, err
	}
	return kbzpayList, nil
}

func (model *TenantChannelKbzpayModel) GetKbzpayIsLimit() (int32, error) {
	var kbzpay schema.TenantChannelKbzpay
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).First(&kbzpay)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetKbzpayIsLimit,err", err)
		return 0, err
	}
	return kbzpay.IsLimit, nil
}

func (model *TenantChannelKbzpayModel) UpdateKbzpayIsLimit(isLimit int32) error {
	var total int64
	model.getDb().Model(&schema.TenantChannelKbzpay{}).Count(&total)
	if total == 0 {
		return nil
	}
	updates := schema.TenantChannelKbzpay{
		IsLimit: isLimit,
	}
	res := model.getDb().Model(&schema.TenantChannelKbzpay{}).Where("id>0").Updates(updates)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateKbzpayIsLimit,err", err)
		return err
	}
	return nil
}
